// 初始化仓库
export const initState = {
    user: {
        phone: "",
        password: "",
    },
    info:sessionStorage.getItem("user")?JSON.parse(sessionStorage.getItem("user")):{}
}

export const reducer = (state, action) => {
    switch (action.type) {
        case "changeUser":
            return {
                ...state,
                user: {
                    ...state.user,
                    [action.key]: action.value
                }
            }
        case "changeInfo":
            return {
                ...state,
                info:action.info
            }
        default:
            return state
    }
}

export const actions = {
    changeUser:(key,value)=>({type:"changeUser",key,value}),
    changeInfo:(info)=>{
        if(info.token){
            sessionStorage.setItem("user",JSON.stringify(info))
        }else{
            sessionStorage.removeItem("user")
        }
        return {type:"changeUser",info}
    },
}